www.gusucode.com > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序 > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序\code\PhManage\DutyInfoDlg.cpp

    //Download by http://www.NewXing.com
// DutyInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PhysicM.h"
#include "DutyInfoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDutyInfoDlg dialog


CDutyInfoDlg::CDutyInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDutyInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDutyInfoDlg)
	m_jc = _T("");
	m_demo = _T("");
	m_name = _T("");
	//}}AFX_DATA_INIT
}


void CDutyInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDutyInfoDlg)
	DDX_Control(pDX, IDC_DUTY_LIST, m_List);
	DDX_Text(pDX, IDC_DUTY_JC, m_jc);
	DDX_Text(pDX, IDC_DEMO_EDIT, m_demo);
	DDX_Text(pDX, IDC_DUTY_NAME, m_name);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDutyInfoDlg, CDialog)
	//{{AFX_MSG_MAP(CDutyInfoDlg)
	ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
	ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDutyInfoDlg message handlers

BOOL CDutyInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_List.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	char *head[]={"序 号","职务名称","职务简称","职务ID","备注"};
	int i;
	for (i=0;i<5;i++)
		m_List.InsertColumn(i,head[i],LVCFMT_LEFT,100);//(i==1) ? 150 : 100,i);

	ImageList=new CImageList();
	ImageList->Create(16,16,TRUE | ILC_COLOR32,2,0);
	ImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONUSER));
	ImageList->Add(AfxGetApp()->LoadIcon(IDR_MENUVIEW_TMPL));
	m_List.SetImageList(ImageList,LVSIL_SMALL);// LVSIL_NORMAL);
	Init();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CDutyInfoDlg::DestroyWindow() 
{
   if (ImageList) delete ImageList;	
	return CDialog::DestroyWindow();
}
void CDutyInfoDlg::Init()
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	try{
		DB.Open("select duty_id,duty_name dn,duty_jc dj,demo dm from duty order by duty_id");
		int id=0,no=0;;
		char pname[40],tmp[50];
		memset(pname,0,40);
		while(!DB.isEof())
		{
			sprintf(tmp,"%03d",no+1);
			m_List.InsertItem(no,tmp,0);
			DB.GetValue("dn",pname);
			m_List.SetItemText(no,1,(LPCTSTR)pname);
			DB.GetValue("dj",pname);
			m_List.SetItemText(no,2,(LPCTSTR)pname);
			id=DB.GetValue("duty_id");
			sprintf(pname,"%03d",id);
			m_List.SetItemText(no,3,(LPCTSTR)pname);
			memset(pname,0,40);
			DB.GetValue("dm",pname);
			m_List.SetItemText(no,4,(LPCTSTR)pname);
			no++;
			DB.Next();
		}
	}catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch (...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}
}

void CDutyInfoDlg::AddInfo(CString m_pname,CString m_pj,CString m_dm)
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	int max_id=0;
	try{
		DB.Open("select IDENT_CURRENT('duty') did "
			" from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='duty' ");
		double id=0;
		DB.GetValue("did",&id);
		max_id=(int)id+1;
		DB.ExecSQL("INSERT INTO duty (duty_name,duty_jc,demo) "
			"VALUES('%s','%s','%s')",m_pname,m_pj,m_dm);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddLog("新增职务:%s",m_pname);
		char no[4];
		int icount=m_List.GetItemCount();
		memset(no,0,4);
		sprintf(no,"%03d",icount+1);
		m_List.InsertItem(m_List.GetItemCount(),no,0);
		m_List.SetItemText(icount,1,(LPCTSTR)m_pname);
		m_List.SetItemText(icount,2,(LPCTSTR)m_pj);
		sprintf(no,"%03d",max_id);
		m_List.SetItemText(icount,3,(LPCTSTR)no);
		m_List.SetItemText(icount,4,(LPCTSTR)m_dm);

	}
	catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch(...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}
}

void CDutyInfoDlg::OnAddButton() 
{
	UpdateData();
	m_name.TrimLeft();
	m_name.TrimRight();
	if(m_name.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("职务名称不能为空,请输入");
		return;
	}
	AddInfo(m_name,m_jc,m_demo);
	
}

void CDutyInfoDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CDutyInfoDlg::OnDeleteButton() 
{
   ((CPhysicMApp *)AfxGetApp())->Delete("duty","duty_id",&m_List,3);
}